import MyCrypto from "../../MyCrypto";

export interface IResult {
    [index:string]:string
}

export interface ICONDITION {
    [index:string]:string|number
}


export default class Query{
    private _knex;
    private _crypto = new MyCrypto();
    constructor(props){
        const {knex} = props;
        this._knex = knex;
    }

    /**
     * @param TABLE_NAME{string} 要查询的表明
     * @param COLUMN 要查询的列
     * */
    async query(TABLE_NAME:string, COLUMN?:string[]|string){
        const _CONDITION = {is_active:'1'};
        if(COLUMN === undefined){
            let result = await this._knex.where(_CONDITION).select().from(TABLE_NAME);
            return this.parse(result);
        }
        if(Array.isArray(COLUMN)){
            let result = await this._knex.where(_CONDITION).select(...COLUMN).from(TABLE_NAME);
            return this.parse(result);
        }
        let result = await this._knex.where(_CONDITION).select(COLUMN).from(TABLE_NAME);
        return this.parse(result);
    }


    /**
     * @param TABLE_NAME{string} 要查询的表明
     * @param CONDITION where条件
     * @param COLUMN 要查询的列
     * is_active不需要编码,只接受'0'和'1'
     * 该函数不会对查询的数据进行编码
     * */
    async queryWhereNoneConditionEncode(TABLE_NAME:string, CONDITION:ICONDITION, COLUMN?:string[]|string){
        const _CONDITION = CONDITION;
        _CONDITION.is_active = '1';
        if(COLUMN === undefined){
            let result = await this._knex.where(_CONDITION).select().from(TABLE_NAME);
            return this.parse(result);
        }
        if(Array.isArray(COLUMN)){
            let result = await this._knex.where(_CONDITION).select(...COLUMN).from(TABLE_NAME);
            return this.parse(result);
        }
        let result = await this._knex.where(_CONDITION).select(COLUMN).from(TABLE_NAME);
        return this.parse(result);
    }

    /**
     * @param TABLE_NAME{string} 要查询的表明
     * @param CONDITION where条件
     * @param COLUMN 要查询的列
     * is_active不需要编码,只接受'0'和'1'
     * */
    async queryWhere(TABLE_NAME:string, CONDITION:ICONDITION, COLUMN?:string[]|string){
        const _CONDITION = this.dataEncode(CONDITION);
        _CONDITION.is_active = '1';
        if(COLUMN === undefined){
            let result = await this._knex.where(_CONDITION).select().from(TABLE_NAME);
            return this.parse(result);
        }
        if(Array.isArray(COLUMN)){
            let result = await this._knex.where(_CONDITION).select(...COLUMN).from(TABLE_NAME);
            return this.parse(result);
        }
        let result = await this._knex.where(_CONDITION).select(COLUMN).from(TABLE_NAME);
        return this.parse(result);
    }

    private parse(data:IResult[]):IResult[]{
        let _resultList:IResult[] = [];
        for(let i=0;i<data.length;i++){
            let _data:IResult = {};
            let _result:IResult = {};
            _data = data[i];
            const KEYS = Object.keys(_data);
            const LENGTH = KEYS.length;
            for(let k=0;k<LENGTH;k++){
                const KEY = KEYS[k];
                if(typeof _data[KEY]==='string'){
                    _result[KEY] = this.decode(_data[KEY])
                }else{
                    _result[KEY] = _data[KEY];
                }

            }
            _resultList.push(_result);
        }
        return _resultList;
    }

    private dataEncode(data){
        return this._crypto.dataEncode(data);
    }

    private decode(DATA:string){
        return this._crypto.strDecode(DATA);
    }

}
